<template>
  <div class="app-container">
    <div class="filter-container" v-if="false">
      <el-input
        style="width: 135px;"
        class="filter-item"
        placeholder="请设备编号"
        v-model="queryForm.deviceCode"
      >
      </el-input>
      <el-input
        style="width: 120px;"
        class="filter-item"
        placeholder="请输入文件名称"
        v-model="queryForm.fileName"
      >
      </el-input>
      <el-input
        style="width: 120px;"
        class="filter-item"
        placeholder="请输入文件版本"
        v-model="queryForm.fileVer"
      >
      </el-input>
      <el-select
        clearable
        style="width: 150px"
        class="filter-item"
        v-model="queryForm.status"
        placeholder="请选择升级结果"
      >
        <el-option
          v-for="(val, key) in Dicts.UPGRADED_RESULT"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <!--<el-button v-permission="'updateDetail:massNotify'" class="filter-item" @click="notifyByIds" type="success" icon="el-icon-message">批量通知</el-button> -->
    </div>

    <div class="filter-container">
      <el-input
        style="width: 160px;"
        class="filter-item"
        placeholder="请输入设备名称"
        v-model="queryForm.deviceName"
      >
      </el-input>

      <el-button
        class="filter-item"
        @click="filterItems"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
    </div>

    <el-table
      v-loading="loading"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
      :data="records"
      style="width: 100%"
    >
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="升级时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="updateStatus" label="更新状态" align="center">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.updateStatus === 0"
            >更新中</el-tag
          >
          <el-tag
            size="small"
            type="success"
            v-if="scope.row.updateStatus === 1"
            >更新成功</el-tag
          >
          <el-tag size="small" type="danger" v-if="scope.row.updateStatus === 2"
            >更新失败</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="deviceName" label="设备名称" align="center">
      </el-table-column>
      <el-table-column prop="serverName" label="服务器" align="center">
      </el-table-column>
      <el-table-column prop="packageName" label="更新包" align="center">
      </el-table-column>
      <el-table-column prop="versionNumber" label="版本号" align="center">
      </el-table-column>
      <el-table-column prop="userName" label="操作人" align="center">
      </el-table-column>
      <!-- <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
        width="250"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'updateDetail:notify'"
            size="mini"
            type="success"
            v-if="scope.row.step == -1"
            @click="notifyById(scope.row)"
            title="通知"
            icon="el-icon-message"
            circle
          ></el-button>
          <el-button
            v-permission="'updateDetail:toNotify'"
            size="mini"
            type="success"
            v-if="scope.row.step == 4 || scope.row.step == 7"
            @click="notifyById(scope.row)"
            title="重新通知"
            icon="el-icon-refresh"
            circle
          ></el-button>
          <el-button
            v-permission="'updateDetail:delete'"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            title="删除"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  queryUpdateDetail,
  deleteUpdateDetail,
  notifyById,
  notifyByIds
} from '@/api/terminal'

import { getOtaLogs } from '@/api/ota'

export default {
  name: 'updateDetail',
  data() {
    return {
      loading: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        title: undefined,
        deviceName: ''
      },
      records: []
    }
  },

  created() {
    this.doQuery()
  },

  computed: {
    filteredItems() {
      if (this.queryForm.deviceName) {
        return this.records.filter(item =>
          item.deviceName
            .toLowerCase()
            .includes(this.queryForm.deviceName.toLowerCase())
        )
      }
      return this.records
    }
  },

  methods: {
    filterItems() {
      if (this.queryForm.deviceName) {
        this.records = this.records.filter(item =>
          item.deviceName
            .toLowerCase()
            .includes(this.queryForm.deviceName.toLowerCase())
        )
      } else {
        this.doQuery()
      }
    },

    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      const params = {
        ...this.queryForm
      }
      params.page = this.queryForm.current
      delete params.current
      getOtaLogs({
        ...params
      }).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    handleDelete(rowId) {
      this.$confirm('此操作将删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUpdateDetail(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    notifyById(row) {
      this.$confirm('此操作将下发通知升级, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        notifyById(row).then(({ data: { code, data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: code === 0 ? 'success' : 'error'
          })
        })
      })
    },
    notifyByIds() {
      if (
        this.multipleSelection === undefined ||
        this.multipleSelection.length === 0
      ) {
        this.$message({
          showClose: true,
          message: '请勾选需要通知的门禁设备',
          type: 'warning'
        })
      } else {
        const rowIds = []
        const deviceCodes = []
        for (let i = 0; i < this.multipleSelection.length; i++) {
          if (
            this.multipleSelection[i].step === '-1' ||
            this.multipleSelection[i].step === '4' ||
            this.multipleSelection[i].step === '7'
          ) {
            rowIds.push(this.multipleSelection[i].rowId)
            deviceCodes.push(this.multipleSelection[i].deviceCode)
          }
        }

        if (rowIds.length === 0) {
          this.$message({
            showClose: true,
            message: '操作失败, 只能通知待升级或者升级失败的门禁设备',
            type: 'warning'
          })
        } else {
          this.$confirm(
            '此操作将通知' +
              rowIds.length +
              '台门禁设备升级(只能通知待升级或者升级失败的门禁设备), 是否继续?',
            '提示',
            {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }
          ).then(() => {
            notifyByIds(rowIds, deviceCodes).then(
              ({ data: { code, data, msg } }) => {
                this.doQuery()
                this.$message({
                  showClose: true,
                  message: msg,
                  type: code === 0 ? 'success' : 'error'
                })
              }
            )
          })
        }
      }
    }
  },
  components: {}
}
</script>

<style scoped></style>
